package cn.lecosa.spark.oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class AddDataToDB {

	/**
	 * 十万条数据用jdbc插入数据库 怎么实现
	 * @throws ParseException
	 *              
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @Title: main
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param args 设定文件
	 * @return void 返回类型
	 * @date 2013-1-20 下午3:34:16
	 * @throws
	 */

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException,
			ParseException {
		// TODO Auto-generated method stub

		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
		String url = "*******";
		String uid = "************";
		String pwd = "*********";
		Connection conn = DriverManager.getConnection(url, uid, pwd);
		String sql = "INSERT INTO TableNameT VALUES(?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		int recordNum = 0; // 计数器
		int commit_size = 5000;// 每次提交记录数5
		for (int i = 33439; i <= 99999; i++) {
			conn.setAutoCommit(false);// 设置数据手动提交，自己管理事务  
			recordNum++; // 计数
			pstmt.setString(1, "苏F" + i);
			pstmt.setString(2, "蓝牌");
			pstmt.setString(3, "小车");
			pstmt.setInt(4, 345);
			pstmt.setString(5, "asdf");
			pstmt.setString(6, "12345678900");
			pstmt.setString(7, "dadf");
			String str = "2013-01-15 18:18:18";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			pstmt.setTimestamp(8, new Timestamp(sdf.parse(str).getTime()));
			pstmt.setString(9, "adf");
			pstmt.addBatch();
			if (recordNum % commit_size == 0) {
				pstmt.executeBatch();
				conn.commit();
				conn.close();
				conn = DriverManager.getConnection(url, uid, pwd);
				conn.setAutoCommit(false);
				pstmt = conn.prepareStatement(sql);
			}
		}
		if (recordNum % commit_size != 0) {
			pstmt.executeBatch();
			conn.commit();
			System.out.println("提交:" + recordNum);
		}
		pstmt.close();
		conn.close();
		System.out.println("insert success!!!");

	}

}
